package com.iterator.service.wechat.lc;

import com.iterator.bean.web.pojo.dhy.Keyword;
import com.iterator.bean.web.pojo.dhy.KeywordExample;
import com.iterator.bean.web.pojo.lyh.User;
import com.iterator.bean.web.pojo.lyh.MallUserExample;
import com.iterator.bean.wechat.lc.po.SearchHistory;
import com.iterator.bean.wechat.lc.po.SearchHistoryExample;
import com.iterator.bean.wechat.lc.SearchIndexVO;
import com.iterator.mapper.web.marketManager.KeywordMapper;
import com.iterator.mapper.web.userManager.MallUserMapper;
import com.iterator.mapper.wechat.lc.CustomKeywordMapperWX;
import com.iterator.mapper.wechat.lc.SearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    KeywordMapper keywordMapper;
    @Autowired
    CustomKeywordMapperWX customKeywordMapperWX;
    @Autowired
    MallUserMapper userMapper;
    @Autowired
    SearchHistoryMapper historyMapper;

    @Override
    public SearchIndexVO getIndex(String username) {
        SearchIndexVO indexVO = new SearchIndexVO();
        //查HistoryKeyword
        //username判空，不登录也可访问此api

        if(username!=null &&  !(username=="")){
            MallUserExample userExample = new MallUserExample();
            MallUserExample.Criteria criteria = userExample.createCriteria();
            criteria.andUsernameEqualTo(username);
            User userSelect = userMapper.selectByExample(userExample).get(0);
            SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
            SearchHistoryExample.Criteria criteria1 = searchHistoryExample.createCriteria();
            criteria1.andUserIdEqualTo(userSelect.getId());
            searchHistoryExample.setOrderByClause("update_time desc");
            List<SearchHistory> histories = historyMapper.selectByExample(searchHistoryExample);
            for (SearchHistory history : histories) {
                indexVO.addHistoryKeyword(history.getKeyword());
            }
        }

        //查keyword
        LinkedList<Keyword> keywords = customKeywordMapperWX.selectkeyword();
        for (Keyword keyword : keywords) {
            if(keyword.getIsDefault()){
                indexVO.setDefaultKeyword(keyword);
                break;
            }
        }
        indexVO.setHotKeywordList(keywords);
        return indexVO;
    }

    @Override
    public void clearSearchHistory(String username) {
        MallUserExample userExample = new MallUserExample();
        MallUserExample.Criteria userCriteria = userExample.createCriteria();
        userCriteria.andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        User user = users.get(0);
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(user.getId());
        int i = historyMapper.deleteByExample(searchHistoryExample);
    }

    @Override
    public List<String> getHelper(String keyword) {
        //使用若干空格分割字符串
        String[] keywords = keyword.split("\\s+");
        LinkedList<String> keywordList = new LinkedList<>();
        //数组转化为collection
        Collections.addAll(keywordList,keywords);
        //复制一个keywordlist给新的循环用
        LinkedList<String> keywordListCopy = new LinkedList<>();
        for (String s : keywordList) {
            keywordListCopy.add(s);
        }
        LinkedList<String> helpers = new LinkedList<>();

        //TODO criteria 和example api


        //包含所有关键词的结果和包含部分关键词的结果
        for (int i = keywordList.size(); i >= 1; i--) {
            KeywordExample keywordExample = new KeywordExample();
            KeywordExample.Criteria criteria = keywordExample.createCriteria();
            for (String key : keywordList) {
                criteria.andKeywordLike("%" + key + "%");
            }
            addHelper(helpers, keywordExample);
            keywordList.remove(i-1);
        }
        //包含单个关键词的结果
        KeywordExample keywordExample1 = new KeywordExample();
        if(keywordListCopy.size()!=0 || keywordListCopy!= null){
            for (String key : keywordListCopy) {
                KeywordExample.Criteria criteria = keywordExample1.createCriteria();
                criteria.andKeywordLike("%" + key + "%");
                keywordExample1.or(criteria);
            }
            addHelper(helpers, keywordExample1);
        }
        return helpers;

    }

    private LinkedList<String> addHelper(LinkedList<String> helpers, KeywordExample keywordExample) {
        keywordExample.createCriteria().andDeletedEqualTo(false);
        keywordExample.setOrderByClause("sort_order desc,update_time desc");
        List<Keyword> keywordsSelected = keywordMapper.selectByExample(keywordExample);
        for (Keyword keyword1 : keywordsSelected) {
            if (!helpers.contains(keyword1.getKeyword())) {
                helpers.add(keyword1.getKeyword());
            }
        }
        return helpers;
    }
}
